import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/models/invite_settles_entity.dart';
import 'package:seedfour/app/theme.dart';

import '../../../constants.dart';
import 'logic.dart';

class MineInviteSettlesPage extends StatelessWidget {
  MineInviteSettlesPage({Key? key}) : super(key: key);

  final logic = Get.find<MineInviteSettlesLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('邀请奖励'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
      ),
      body: _buildListItemBg(),
    );
  }

  Widget _valueText(String? value) {
    return SizedBox(
      width: Get.width / 2,
      child: Text(
        textAlign: TextAlign.right,
        value ?? '',
        style: TextStyle(
          fontSize: kFontSizeSmall,
          color: AppColor.colorFontBlack,
        ),
      ),
    );
  }

  Widget _labelText(String value) {
    return Text(
      value,
      style: TextStyle(
        fontSize: kFontSizeSmall,
        color: AppColor.colorFontBlack,
      ),
    );
  }

  List<Widget> _itemBuilder(InviteSettlesData item) {
    return [
      Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _labelText('奖励金额(1%订单金额)'),
            Expanded(child: _valueText(item.refeAmount.toString())),
          ]),
      SizedBox(height: kSpaceLarge),
      Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _labelText('订单金额'),
            _valueText(item.orderAmount.toString())
          ]),
      SizedBox(height: kSpaceLarge),
      Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [_labelText('订单客户'), _valueText(item.title)]),
      SizedBox(height: kSpaceLarge),
      Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [_labelText('邀请对象'), _valueText(item.refereeName)]),
      SizedBox(height: kSpaceLarge),
      Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            _labelText('订单时间'),
            _valueText(DateFormat('yyyy.MM.dd HH:mm').format(
                DateTime.fromMillisecondsSinceEpoch(item.orderTimestamp!)))
          ]),
    ];
  }

  Widget _renderRow(BuildContext context, int index) {
    if (index < logic.inviteSettles.length) {
      return Container(
        margin: EdgeInsets.only(
            left: kSpaceSmall,
            right: kSpaceSmall,
            top: kSpaceMedium,
            bottom: index == logic.inviteSettles.length - 1 ? kSpaceMedium : 0),
        padding: EdgeInsets.all(kSpaceMedium),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(kRadiusMedium),
        ),
        child: Column(
          children: _itemBuilder(logic.inviteSettles[index]),
        ),
      );
    } else {
      return _getMoreWidget();
    }
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(kSpaceMedium),
        child: Text(
          logic.bottomTips.value,
          style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey),
        ),
      ),
    );
  }

  Widget _buildListItemBg() {
    return Obx(() {
      if (logic.isLoading.value) {
        return const Center(child: CircularProgressIndicator());
      } else {
        return ListView.builder(
          itemBuilder: _renderRow,
          itemCount: logic.inviteSettles.length + 1,
          controller: logic.scrollController,
        );
      }
    });
  }
}
